<style type="text/css">

.button{background:url(images/left-menu-li.gif) repeat-x left top #F1F6F9; border:1px solid #318DAC; font-weight:bold; padding:2px 5px; font-weight:lighter; color:#318DAC; vertical-align:middle; -moz-border-radius:3px; -webkit-border-radius:3px; margin:auto 5px}

#listContent{
	width:670px;
	height:500px;
	overflow-y:scroll;
}
.mTitle{
	width:640px;
	line-height: 25px;
	background-color:#B0C4DE;
	margin-bottom:4px;
	clear :both;
}
.actionBox{
	width:635px;
}
.actDiv {
    float: left;
    line-height: 25px;
    margin-left: 13px;
    width: 190px;
}
</style>
<script type="text/javascript">
$(function(){
	loadData();	
	window.parent.$(".dialogIframe").dialog( "option", 
		"buttons", { 
			"保存": function() {
				save();				
			},
			"<{$lang.close}>": function() { 
				window.parent.$(".dialogIframe").dialog("close");
			},
		}).width(680).height(550);
		
});

function showHide(module){
	if($('.'+module).css('display')=='none'){
		$('.'+module).show();
	}else{
		$('.'+module).hide();
	}	
}

function loadData(){
	var oldIdArr=[<{$rightIdArr}>];
	loading();
	$.ajax({ //这里使用到Jquery的ajax<{$lang.method}>
		type: "POST",
		async: false,
		dataType: "json",
		url: "/user/position/rights", //请求的处理url
		data:{'act':'loadData',},
		success: function(json) {
			var list = '';
			var module = '';
			if(json.ask != 1){
				list = "<div class=''>&nbsp;<{$lang.no_data}></div>";
			}else {
				$.each(json.result, function(key, val){
					if(val.ur_module!= module){
						if(val.ur_module!= module && module != ''){
							list += "</div>";	//每个<{$lang.module}>后的结束DIV<{$lang.label}>
						}
						module = val.ur_module;
											
						list += "<div class='mTitle' onclick=\"showHide('"+module+"')\">";						
						list += '<input type="checkbox" onclick="checkAll(this)" ref="checkbox'+module+'"><{$lang.module}>名：'+module;
						list += "</div>";
						list += "<div class='actionBox "+module+"'>";	
					}
					
					list +="<div class='actDiv'>";
					if( inArray(val.ur_id,oldIdArr)){
						list += "<input type='checkbox' value='"+val.ur_id+"' name='rights' checked='checked' class='checkbox"+module+"' />"+val.ur_name;						
					}else{
						list += "<input type='checkbox' value='"+val.ur_id+"' name='rights' class='checkbox"+module+"' />"+val.ur_name;
					}
					if(val.ur_description){
						list += '( ' + val.ur_description + ' )';
					}
					
					list += "</div>";
					if( key==(json.result.length-1) ){
						list += "</div>";
					}
					
				});
				$("#listContent").html(list);
				closeLoading();
			}
		}
	}); 
}

function save(){	
	var user_id =  $('[name=user_id]').val();
	var rights = [];
	$('input:checkbox[name="rights"][checked]').each(function(i){
		rights[i]=$(this).val();		
    });	
	if(!rights.length){
		alert('至少<{$lang.pls}>一项');
		return;
	}
	var tip = '';
	$.ajax({
			type: "POST",
			async: false,
			dataType: "json",
			url: "/user/user/saverights", //请求的处理url
			data:{'rights':rights,'user_id':user_id},
			success: function(json) {
				if(json==null){
					window.parent.$(".dialogIframe").dialog("close");
					return;
				}
				if(json.ask){
					tip = '<{$lang.operation}><{$lang.success}>';					
				}else{
					tip = '<{$lang.operation}><{$lang.fail}>';	
				}
				window.parent.$(".dialogIframe").dialog("close");
			}
	});
	
}

function checkAll(obj){
	var cname = $(obj).attr('ref');
	if($(obj).attr("checked")=='checked'){
		$('.'+cname).attr('checked','true');
		
	}else{
		$('.'+cname).attr('checked',false);
	}
}

function inArray(val,arr){	//在数组中<{$lang.search}>某元素，存在则返回1，否则返回0；
	var chk = 0;
	for(i=0;i<arr.length;i++){
		if(arr[i]==val){
			return 1;
		}else{
			chk = 0;
		}
	}
	return chk;
}

function closeIfarme(){
	window.parent.$(".dialogIframe").dialog("close");
}

</script>
<div class="header" style="font-size:13px;font-weight:bold;margin:4px;">
	请在下面的复选框中<{$lang.pls}>相应的<{$lang.function}>
</div>
	<div id="action_list_dialog" class="" style="width: auto; min-height: 0px;">

		<form id="actionForm" name="actionForm">
			<div id="listContent">
				
			</div>	<!-- listContent 结束 -->
			<div>
				<input type="hidden" name="user_id" value="<{$user_id}>" />
			</div>
		</form>
		
	</div><!-- action_list_dialog 结束 -->